package com.itchen.controller.module;

import com.itchen.annotation.Log;
import com.itchen.common.Result;
import com.itchen.domain.dto.PermissionDTO;
import com.itchen.domain.entity.Permission;
import com.itchen.service.PermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理控制器
 */
@RestController
@RequestMapping("/system/permission")
@Tag(name = "权限管理接口", description = "权限的增删改查接口")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 获取权限树
     */
    @GetMapping("/tree")
    @Operation(summary = "获取权限树", description = "获取所有权限的树形结构")
    @Log(operationType = "查询", operationModule = "菜单管理")
    public Result<List<PermissionDTO>> getPermissionTree() {
        return Result.success(permissionService.getPermissionTree());
    }

    /**
     * 获取用户菜单
     */
    @GetMapping("/user/menu")
    @Operation(summary = "获取用户菜单", description = "获取当前登录用户的菜单权限")
    public Result<List<PermissionDTO>> getUserMenu() {
        return Result.success(permissionService.getUserMenu());
    }

    /**
     * 获取所有权限列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取权限列表", description = "获取所有权限列表")
    public Result<List<Permission>> listAllPermissions() {
        return Result.success(permissionService.listAllPermissions());
    }

    /**
     * 根据ID获取权限
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取权限详情", description = "根据ID获取权限详情")
    public Result<Permission> getPermissionById(@PathVariable Integer id) {
        return Result.success(permissionService.getPermissionById(id));
    }

    /**
     * 添加权限
     */
    @PostMapping
    @Operation(summary = "添加权限", description = "添加新的权限")
    public Result<Boolean> addPermission(@RequestBody Permission permission) {
        return Result.success(permissionService.addPermission(permission));
    }

    /**
     * 更新权限
     */
    @PutMapping
    @Operation(summary = "更新权限", description = "更新已有权限")
    @Log(operationType = "修改", operationModule = "菜单管理")
    public Result<Boolean> updatePermission(@RequestBody Permission permission) {
        return Result.success(permissionService.updatePermission(permission));
    }

    /**
     * 删除权限
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除权限", description = "删除权限及其子权限")
    @Log(operationType = "删除", operationModule = "菜单管理")
    public Result<Boolean> deletePermission(@PathVariable Integer id) {
        return Result.success(permissionService.deletePermission(id));
    }
} 